﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace QL_NOI_TRU
{
    public partial class frmNhapTTBenhAn : DevExpress.XtraEditors.XtraForm
    {
        public frmNhapTTBenhAn()
        {
            InitializeComponent();
        }

        public static string name ="";

        private void frmNhapTTBenhAn_Load(object sender, EventArgs e)
        {
            //đổ dữ liệu vào gridview
            var kn = new Ketnoi(frmDangnhap.stringConn);
            gridControl1.DataSource = kn.ExcuteQuery("select MABENHAN,MA_BN,MA_KHOA,MA_PHONG,MA_GIUONG,TIENSU,BENHSU,LYDONHAPVIEN,SOTHEBHYT,NGAYNHAPVIEN,MA_NV from BENH_AN");

            //lấy tên đăng nhập
            tbnhanvienlap.Text = name;

            // đổ dữ liệu vào cbbMaBenhNhan
            var maBN = "select MA_BN,HOTEN from BENH_NHAN";
            SqlConnection conn = new SqlConnection(frmDangnhap.stringConn);
            SqlCommand cm = conn.CreateCommand();
            cm.CommandText = maBN;
            conn.Open();
            SqlDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {
                var newVariable = cbbMABenhNhan.Properties.Items.Add(String.Format("{0} | {1}", dr["MA_BN"], dr["HOTEN"]));
            }

        }

        private void btThem_Click(object sender, EventArgs e)
        {
            var ob = new obTTBenhAN();
            ob.MABENHAN = tbMABenhAN.Text;
            string mkhoa = cbbKhoa.SelectedItem.ToString();
            string m_khoa = mkhoa.Substring(0, 2);
            ob.MAKHOA = m_khoa;
            ob.MAPHONG = cbbPhong.SelectedItem.ToString().Substring(0, 2);
            ob.MAGIUONG = cbbgiuong.SelectedItem.ToString().Substring(0, 2);
            ob.TIENSU = meTienSu.Text;
            ob.BENHSU = meBenhSu.Text;
            ob.LYDO = txtLyDoNV.Text;
            ob.SOBHYT = tbsoBHYT.Text;
            ob.NGAYNHAPVIEN = cbbngaynhapvien.Text;
            ob.NHANVIENLAP = tbnhanvienlap.Text;
            var ob1 = new obTTBenhNhan();
            ob1.MABENHNHAN = cbbMABenhNhan.SelectedItem.ToString().Substring(0, 2);

            if (ob.MABENHAN != "" && ob1.MABENHNHAN != "")
            {
                string a = String.Format("Insert into BENH_AN(MABENHAN,MA_BN,MA_KHOA,MA_PHONG,MA_GIUONG,TIENSU,BENHSU,LYDONHAPVIEN,SOTHEBHYT,NGAYNHAPVIEN,MA_NV) VALUES('{0}', '{1}','{2}','{3}','{4}',N'{5}',N'{6}',N'{7}','{8}','{9}','{10}", ob.MABENHAN, ob1.MABENHNHAN, ob.MAKHOA, ob.MAPHONG, ob.MAGIUONG, ob.TIENSU, ob.BENHSU, ob.LYDO, ob.SOBHYT, ob.NGAYNHAPVIEN, ob.NHANVIENLAP) + "')";
                var kn = new Ketnoi(frmDangnhap.stringConn);
                kn.ExcuteNonQuery(a);
                btRefresh_Click(null, null);
                MessageBox.Show("Đã thêm thành công!");
            }
            else
                MessageBox.Show("vui lòng nhập đầy đủ mã bệnh án và mã bệnh nhân!");
        }

        private void btRefresh_Click(object sender, EventArgs e)
        {
            tbMABenhAN.Text = "";
            cbbMABenhNhan.SelectedIndex = -1;
            cbbKhoa.SelectedIndex = -1;
            cbbPhong.SelectedIndex = -1;
            cbbgiuong.SelectedIndex = -1;
            meTienSu.Text = "";
            meBenhSu.Text = "";
            txtLyDoNV.Text = "";
            tbnhanvienlap.Text = "";
            cbbngaynhapvien.Text = "";
            tbsoBHYT.Text = "";
            tbnhanvienlap.Text = "";

            frmNhapTTBenhAn_Load(null, null);
        }

        private void cbbMABenhNhan_SelectedIndexChanged(object sender, EventArgs e)
        {
            // đỗ dữ liệu vào cbbKhoa

            var maK = "select * from KHOA";
            SqlConnection conn = new SqlConnection(frmDangnhap.stringConn);
            SqlCommand cm = conn.CreateCommand();
            cm.CommandText = maK;
            conn.Open();
            SqlDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {
                var newVariable = cbbKhoa.Properties.Items.Add(String.Format("{0} | {1}", dr["MA_KHOA"], dr["TEN_KHOA"]));
            }
        }

        private void cbbKhoa_SelectedIndexChanged(object sender, EventArgs e)
        {
            string a = cbbKhoa.SelectedItem.ToString().Substring(0, 2);
            var maP = String.Format("select * from PHONG_BENH where MA_KHOA = '{0}'", a);
            SqlConnection conn = new SqlConnection(frmDangnhap.stringConn);
            SqlCommand cm = conn.CreateCommand();
            cm.CommandText = maP;
            conn.Open();
            SqlDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {
                var newVariable = cbbPhong.Properties.Items.Add(String.Format("{0} | {1} | {2} | {3}", dr["MAPHONG"], dr["MA_KHOA"],dr["GIATIEN"],dr["TRANGTHAI"]));
            }
        }

        private void cbbPhong_SelectedIndexChanged(object sender, EventArgs e)
        {
            string a = cbbPhong.SelectedItem.ToString().Substring(0, 4);
            var maG = String.Format("select * from GIUONG where MAPHONG = '{0}'", a);
            SqlConnection conn = new SqlConnection(frmDangnhap.stringConn);
            SqlCommand cm = conn.CreateCommand();
            cm.CommandText = maG;
            conn.Open();
            SqlDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {
                var newVariable = cbbgiuong.Properties.Items.Add(String.Format("{0} | {1} | {2}", dr["MAPHONG"], dr["MAGIUONG"], dr["TINHTRANG"]));
            }
        }

    }
}